//
//  FPLoginView.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/1/22.
//

#import "FPLoginView.h"

@interface FPLoginView () <UIGestureRecognizerDelegate , TTTAttributedLabelDelegate , UITextFieldDelegate>

@property (nonatomic , strong) NSArray *dataSource;

@end

@implementation FPLoginView

- (instancetype)initWithSource:(NSArray *)source {
    
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    
    if (self) {
    
        self.dataSource = source;
        
        // 设置背景色，确保弹窗视图的背景半透明
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        
        // 获取屏幕的高度
        CGFloat screenHeight = self.bounds.size.height;
        
        // 计算弹窗的高度
        CGFloat popupHeight = 394.f / 375.f * SCREEN_WIDTH;
        
        // 创建弹窗视图
        self.popupView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(FPLoginContentView.class) owner:nil options:nil].lastObject;
        self.popupView.frame = CGRectMake(0, screenHeight, self.bounds.size.width, popupHeight);
        self.popupView.backgroundColor = [UIColor whiteColor];
//        self.popupView.layer.cornerRadius = 20.f;
        [UIView setCornerForView:self.popupView withRadius:20.f bounds:self.popupView.bounds corners:(UIRectCornerTopLeft | UIRectCornerTopRight)];
        self.popupView.layer.masksToBounds = YES;
        
        [self addSubview:self.popupView];
        
        // 添加点击背景关闭弹窗的手势
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidePopup)];
        [self addGestureRecognizer:tapGesture];
        tapGesture.delegate = self;
        
        self.isSelect = NO;
        
        NSMutableString *protocolStr = NSMutableString.string;
        
        for (FPProtocolModel *model in source) {
            
            [protocolStr appendFormat:@"%@",model.agreementName];
        }
        
        NSString *text = [NSString stringWithFormat:@"我已阅读并同意%@", protocolStr];
        
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
        
        for (NSInteger i = 0; i < source.count; i ++) {
            
            FPProtocolModel *model = source[i];
            
            NSRange range = [text rangeOfString:[NSString stringWithFormat:@"%@", model.agreementName]];
            [NSString stringToAttribute:attStr color:MAIN_BLUE_COLOR range:range];
            
            [self.popupView.privacyLab addLinkToURL:[NSURL URLWithString:model.address] withRange:range];
        }
        
        self.popupView.privacyLab.attributedText = attStr;
        self.popupView.privacyLab.userInteractionEnabled = YES;
        self.popupView.privacyLab.delegate = self;
        
        [self.popupView.selectImgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectAction)]];
        [self.popupView.imgBackView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectAction)]];
        
        [self.popupView.loginBtn handleViewControlEvents:UIControlEventTouchUpInside withBlock:^{
           
            self.loginBlock();
        }];
        
        [self.popupView.getCodeBtn handleViewControlEvents:UIControlEventTouchUpInside withBlock:^{
          
            self.getSMSCodeBlock();
        }];
    }
    return self;
}


// 弹窗从下到上的动画
- (void)showPopup {
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame = self.popupView.frame;
        frame.origin.y = self.bounds.size.height - self.popupView.bounds.size.height;
        self.popupView.frame = frame;
    }];
}

// 弹窗关闭的动画
- (void)hidePopup {
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame = self.popupView.frame;
        frame.origin.y = self.bounds.size.height;
        self.popupView.frame = frame;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

//- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
//    // 判断点是否在某个特定视图内
//    UIView *targetView = self.popupView; // 替换为你的子视图
//    if (CGRectContainsPoint(targetView.frame, point)) {
//        // 返回 nil 表示不响应点击事件
//        return nil;
//    }
//    
//    // 默认行为
//    return [super hitTest:point withEvent:event];
//}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    if ([touch.view isEqual:self]) {
        
        self.showTabBarBlock();
        return YES;
    }
    
    return NO;
}


#pragma mark - TTTAttributedLabelDelegate 处理协议点击
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
 
//    [MBManager showText:url.absoluteString];
    if ([self.delegate respondsToSelector:@selector(customViewDidTapPrivacy:)]) {
        
        for (FPProtocolModel *model in self.dataSource) {
            
            if ([url.absoluteString isEqualToString:model.address]) {
                
                [self.delegate customViewDidTapPrivacy:model];
                return;
            }
        }
    }
}

- (void)selectAction {
    
    FeedBackGeneratorAction
    
    self.isSelect = !self.isSelect;
    
    if (self.isSelect) {
     
        self.popupView.selectImgView.image = SELECT_IMG_BLUE;
    }
    else {
     
        self.popupView.selectImgView.image = UNSELECT_IMG;
    }
}

- (void)shakePrivacyView {
    
    FeedBackGeneratorAction
    [self.popupView.selectImgView shake];
    [self.popupView.privacyLab shake];
}



@end
